/*
* =====================================================================================
*
*       Filename:  decimal_to_binary.c
*
*    Description:  
*
*        Version:  1.0
*        Created:  2010年09月18日 14时32分46秒
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  YOUR NAME (), 
*        Company:  
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include "me/assert.h"
#include "me/stack.h"
#include "me/mem.h"



int 
main(int argc, char **argv)
{
	
	if (argc != 2) {
		fprintf(stderr, "error,\n usage: %s decimal_num\n", argv[0]);
		return EXIT_FAILURE;
	}


	long decimal = atol(argv[1]);
	if ( decimal <= 0 ) {
		assert(0);	
	}

	stack_t s= stack_new(0);

	while (1) {
		long tmp = decimal % 2;
		void *p = NEW(p);
		*((long*)p) = tmp;
		stack_push(s, p);
		decimal /= 2;
		if ( decimal == 0 ) {
			break;
		}
	}

	TRY
		while (1) {
			long *p  = stack_pop(s);
			fprintf(stdout, "%ld", *p);
			FREE(p);
		}
	EXCEPT (PopEmptyStack_Failed)
		stack_destroy(&s);
	ELSE
		assert(0);  /*  unreachable stat */
	ENDTRY




	printf("\n");
	fflush(stdout);
	return 0;
}

